package com.yuncheng.oaApi.busService;

import com.yuncheng.oaApi.entity.OaSysRole;
import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.oaApi.constant.OaCommonFlowConstant;
import com.yuncheng.oaApi.constant.OaFlowNodeConstant;
import com.yuncheng.oaApi.entity.OaSysJob;
import com.yuncheng.entity.YcSysOrg;
import com.yuncheng.entity.YcSysUser;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.oaApi.mapper.OaYcSysUserMapper;
import com.yuncheng.oaApi.service.*;
import com.yuncheng.utils.ThreadContextHelper;
import com.yuncheng.oaApi.vo.AgentVo;
import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.vo.GetKsKz;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

@Component(value = "oaFlowNodeDbrService")
public class OaFlowNodeDbrService {

    @Resource
    @Lazy
    private IOaSysRoleService sysRoleService;

    @Resource
    @Lazy
    private OaYcSysUserMapper oaYcSysUserMapper;

    @Resource
    @Lazy
    private IOaYcSysUserService ycSysUserService;

    @Resource
    @Lazy
    private IOaSysJobService sysJobService;

    @Resource
    @Lazy
    private IOaYcSysOrgService ycSysOrgService;

    @Resource
    @Lazy
    private IOaSysUserFgldService sysUserFgldService;

    //获取中心职员
    public Map getzxzy(){
        List<OaSysRole> list = sysRoleService.list(new LambdaQueryWrapper<OaSysRole>().eq(OaSysRole::getRoleName, OaFlowNodeConstant.ROLE_NAME_ZXZY));
        OaSysRole oaSysRoles = list.get(0);
        String zxzyid= oaSysRoles.getId();
        List<YcSysUser> zxzyList = oaYcSysUserMapper.getZxzy(zxzyid);
        Map map = new HashMap();
        String name = "";
        String id = "";
        for (YcSysUser zxzy:zxzyList){
            name += zxzy.getId() + ",";
            id += zxzy.getRealname() + ",";
        }
        map.put("dbrId", id);
        map.put("dbr", name);
        return map;
    }


    //根据岗位id 查询待办人
    public Map getDbrByGwid(String gwIds){
        if (StringUtils.isBlank(gwIds)){
            throw  new BusinessException("岗位不存在");
        }
        Map map = new HashMap();
        String name = "";
        String id = "";
        //查询当前岗位id下的所有人
        List<YcSysUser> gwDbr = oaYcSysUserMapper.getGwDbr(gwIds);
        if (gwDbr == null){
            throw  new BusinessException("岗位不存在");
        }
        for (YcSysUser gw:gwDbr){
            name += gw.getRealname() + ",";
            id += gw.getId() + ",";
        }
        map.put("dbrId", id);
        map.put("dbr", name);
        return map;
    }

    //根据部门id 查询待办人 和待办人id
    public Map getDbrByBmid(String departIds){
        String dbr="";
        String dbrid="";
        Map map = new HashMap();
        List<YcSysUser> userByDepartId = oaYcSysUserMapper.getUserByDepartId(departIds);
        if (userByDepartId == null){
            throw  new BusinessException("用户不存在");
        }
        for (YcSysUser user: userByDepartId){
            if (StringUtils.isBlank(dbr) && StringUtils.isBlank(dbrid)){
                dbr += user.getRealname();
                dbrid += user.getId();
            }else {
                dbr += ","+ user.getRealname();
                dbrid += ","+ user.getId();
            }
            map.put("dbrId",dbrid);
            map.put("dbr",dbr);
        }
        return map;
    }

    //根据部门id 和 岗位id 查询待办人
    public Map getdbrByDeptName(String deptNameid,String gwid){
        YcSysOrg ycSysOrg = ycSysOrgService.getById(deptNameid);
        OaSysJob oaSysJob = sysJobService.getById(gwid);
        String orgName = ycSysOrg.getOrgName();
        String jobName = oaSysJob.getJobName();
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";
        List<GetKsKz> ksKzObject = new ArrayList<>();
        ksKzObject = ycSysUserService.getKsKzObject(orgName,jobName);
        if (CollectionUtils.isNotEmpty(ksKzObject)) {
            for (GetKsKz ksKz : ksKzObject) {
                dbrId += ksKz.getId() + ",";
                dbr += ksKz.getRealname() + ",";
            }
        }

        map.put("dbrId", dbrId);
        map.put("dbr", dbr);
        return map;
    }
    /**
     * 根据部门ID获取部门名称及对应部门下的指定人员信息
     *
     * @param deptId 部门ID
     * @return 包含部门名称及指定人员信息的Map
     */
    public Map getKsFgldByDeptName(String deptId) {
        YcSysOrg ycSysOrg = ycSysOrgService.getById(deptId);
        String deptName = ycSysOrg.getOrgName();

        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";

        YcSysUser fgldUserBydept = sysUserFgldService.getFgldUserBydept(deptName);
        if (fgldUserBydept != null) {
            dbrId = fgldUserBydept.getId();
            dbr = fgldUserBydept.getRealname();
        }

        map.put("dbrId",dbrId);
        map.put("dbr",dbr);
        return map;
    }



    public Map<String, String> getKsFgldByDeptNames(String deptId) {
        if (deptId == null || deptId.isEmpty()) {
            throw new IllegalArgumentException("部门ID不能为空");
        }

        YcSysOrg ycSysOrg = ycSysOrgService.getById(deptId);
        if (ycSysOrg == null) {
            return null; // 或者抛出异常
        }
        String deptName = ycSysOrg.getOrgName();

        Map<String, String> map = new HashMap<>();
        String dbrId = "";
        String dbr = "";

        YcSysUser fgldUserBydept = sysUserFgldService.getFgldUserBydept(deptName);
        if (fgldUserBydept != null) {
            dbrId = fgldUserBydept.getId();
            dbr = fgldUserBydept.getRealname();
        }

        map.put("dbrId", dbrId);
        map.put("dbr", dbr);
        return map;
    }




    //根据待办人id 拼接待办人姓名
    public String getDbrNameById(String ids){
        if (StringUtils.isBlank(ids)){
            throw  new BusinessException("用户id为空");
        }
        String name = "";
        List<String> strings = Arrays.asList(ids.split(",|，"));
        List<YcSysUser> ycSysUsers = ycSysUserService.listByIds(strings);
        if (ycSysUsers == null){
            throw  new BusinessException("用户不存在");
        }
        for (YcSysUser user: ycSysUsers){
            if (StringUtils.isBlank(name)){
                name += user.getRealname();
            }else {
                name += ","+ user.getRealname();
            }
        }
        return name;
    }

    /**
     * 将待办人map转为List
     * @param map 待办人map
     */
    public List<AgentVo> getDbrListByMap(Map map){
        String dbrids = map.get("dbrId").toString(); // 待办人id
        String dbrs = map.get("dbr").toString(); // 待办人

        List<AgentVo> agentList = new ArrayList<>();

        if (org.apache.commons.lang3.StringUtils.isNotBlank(dbrids)) {
            String agentIds[] = dbrids.split(",");
            String agentNames[] = dbrs.split(",");

            for (int i = 0; i < agentIds.length; i++) {
                AgentVo agentVo = new AgentVo();
                agentVo.setDbrid(agentIds[i]);
                agentVo.setDbr(agentNames[i]);
                agentList.add(agentVo);
            }
        }
        return agentList;
    }

    /** 通过部门名称获取id
     * @param departName
     */
    public String getDepartIdByDepartName(String departName) {
        List<YcSysUser> ycSysUserList = ycSysUserService.list(new LambdaQueryWrapper<YcSysUser>().eq(YcSysUser::getOrgName, departName));
        String orgId = ycSysUserList.get(0).getOrgId();
        return orgId;
    }

    //判断当前用户是否是科室负责人
    public boolean brksfzr() {

        CurrentUserVo currentUser = ThreadContextHelper.getCurrentUser();
        String nowid = currentUser.getId();
        Map ksfzrByDeptName = getKsfzrByDeptid(currentUser.getOrgName());

        return ksfzrByDeptName.values().stream().anyMatch(value -> StringUtils.containsIgnoreCase((CharSequence) value, nowid));

    }

    public Map getKsfzrByDeptid(String deptName) {
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";
        List<GetKsKz> ksKzObject = new ArrayList<>();
        ksKzObject = ycSysUserService.getKsKzObject(deptName, OaCommonFlowConstant.ROLE_NAME_KZ);
        if (CollectionUtils.isNotEmpty(ksKzObject)) {
            for (GetKsKz ksKz : ksKzObject) {
                dbrId += ksKz.getId() + ",";
            }
        }
        map.put("dbrId", dbrId);
        return map;
    }

    // 判断当前科室的分管领导和中心领导是否是同一人
    public boolean fgldyzxld(String deptName) {
        String zxldid = "";
        List<YcSysUser> zxzrList = ycSysUserService.list(new LambdaQueryWrapper<YcSysUser>()
                .eq(YcSysUser::getOrgName, OaCommonFlowConstant.ORG_NAME_ZXZR)); // 中心主任
        for (YcSysUser zxld : zxzrList) {
            zxldid = zxld.getId();
        }
        String fgldId = "";
        YcSysUser fgldUserBydept = sysUserFgldService.getFgldUserBydept(deptName); //当前使用科室的部门分管领导
        if (fgldUserBydept != null) {
            fgldId = fgldUserBydept.getId();
        }

        boolean sftyir = zxldid.equals(fgldId);
        return sftyir;
    }

    // 对应科室的 科室负责人
    public Map getKsfzrByDeptName(String deptName){
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";
        List<GetKsKz> ksKzObject = new ArrayList<>();
        ksKzObject = ycSysUserService.getKsKzObject(deptName, OaCommonFlowConstant.ROLE_NAME_KZ);
        if (CollectionUtils.isNotEmpty(ksKzObject)) {
            for (GetKsKz ksKz : ksKzObject) {
                dbrId += ksKz.getId() + ",";
                dbr += ksKz.getRealname() + ",";
            }
        }

        map.put("dbrId", dbrId);
        map.put("dbr", dbr);
        return map;
    }

    //中心领导
    public Map getZxld(){
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";
        // 中心主任
        List<YcSysUser> zxzrList = ycSysUserService.list(new LambdaQueryWrapper<YcSysUser>()
                .eq(YcSysUser::getOrgName, OaCommonFlowConstant.ORG_NAME_ZXZR));
        if (CollectionUtils.isNotEmpty(zxzrList)){
            dbrId = zxzrList.get(0).getId();
            dbr = zxzrList.get(0).getRealname();
        }

        map.put("dbrId",dbrId);
        map.put("dbr",dbr);
        return map;
    }



}
